package main.stack;

import java.util.Stack;

/**
 * @author: kejl
 * @create: 2020-11-08 11:49
 */
public class YouCXiaoKuoHao {
    public boolean isValid(String s) {
        if(s == null || "".equals(s)){
            return false;
        }
        Stack<Character> stack = new Stack<Character>();
        char[] chars = s.toCharArray();
        for(char charstr:chars) {
            if (!stack.empty()) {

                boolean isfindAndPop = false;
                if(stack.peek().charValue()=='(' && charstr ==')'){
                    isfindAndPop = true;
                }

                if(stack.peek().charValue()=='[' && charstr ==']'){
                    isfindAndPop = true;
                }

                if (stack.peek().charValue()=='{' && charstr =='}'){
                    isfindAndPop = true;
                }
                if(isfindAndPop){
                    stack.pop();
                }else{
                    stack.push(charstr);

                }
            } else {
                stack.push(charstr);
            }
        }

        if(stack.empty()){
            return true;
        }else{
            return false;
        }
    }
}
